<?php

namespace App\Http\Controllers\Admin\Auth;

use App\Http\Controllers\Admin\BaseController;
use App\Models\Admin\Role;
use Illuminate\Http\Request;
use DB;

class RoleController extends BaseController
{
    public function toCreate()
    {
        $role = new Role();
        return view('admin.auth.role.role',compact('role'));
    }
    public function toEdit(Role $role)
    {
        if($role->id == 1){
            return redirect()->back()->with('error','超级角色不允许编辑');
        }
        return view('admin.auth.role.role', compact('role'));
    }

    public function save(Request $request)
    {
        if($request->input('id')){
            $cnt1 = Role::where('name', $request->input('name'))->where('id', '!=', $request->input('id'))->count();
            if($cnt1){
                return $this->error('标签已被注册');
            }
            $role = Role::find($request->input('id'));
            $role->name = $request->input('name');
            $role->display_name = $request->input('display_name');
            $role->description = $request->input('description');
        }else{
            $cnt1 = Role::where('name', $request->input('name'))->count();
            if($cnt1){
                return $this->error('标签已被注册');
            }
            $role = new Role();
            $role->name = $request->input('name');
            $role->display_name = $request->input('display_name');
            $role->description = $request->input('description');
        }
        $ret = $role->save();

        if($ret){
            return $this->success('提交成功', route('admin-roles'));
        }else{
            return $this->error();
        }
    }

    public function roles()
    {
        $list = Role::all();
        return view('admin.auth.role.roles', compact('list'));
    }

    public function attachPermission(Request $request)
    {
        $role_id = $request->input('role_id');
        if($role_id == 1){
            return redirect()->back()->with('error','超级角色不允许编辑');
        }
        $ids = $request->input('ids');
        $role = Role::find($role_id);
        $ret = $role->perms()->sync(explode(',', $ids));
        if($ret){
            return $this->success('提交成功');
        }else{
            return $this->error();
        }
    }

    public function select(Request $request)
    {
        $user_id = $request->input('user_id');
        $user_roles = DB::select('select role_id from role_user where user_id = '.$user_id);
        $urs = array();
        foreach ($user_roles as $rp){
            $urs[] = $rp->role_id;
        }
        $roles = DB::select('select * from roles');
        $list = array();
        foreach ($roles as $p){
            $p->checked = in_array($p->id, $urs);
            $list[] = $p;
        }
        return view('admin.auth.role.select', compact('list','user_id'));
    }

    public function delete(Role $role){
        $ret = $role->delete();
        if($ret){
            return $this->success('删除成功');
        }else{
            return $this->error();
        }
    }
}
